﻿using AutoMapper;
using MediatR;
using TraceabilityApp_Api.Application.Command.Breed_养殖_;
using TraceabilityApp_Domain;
using TraceabilityApp_ErrorCode;
using TraceabilityApp_Infrastructure;

namespace TraceabilityApp_Api.Application.Handler.Breed_养殖_
{
    public class DelHealthcareHandler : IRequestHandler<DelHealthcareCommand, ApiResult<int>>
    {
        private readonly IBase<HealthcareModel> healthcareRepo;
        private readonly IMapper mapper;

        public DelHealthcareHandler(IBase<HealthcareModel> healthcareRepo,IMapper mapper)
        {
            this.healthcareRepo = healthcareRepo;
            this.mapper = mapper;
        }
        public Task<ApiResult<int>> Handle(DelHealthcareCommand request, CancellationToken cancellationToken)
        {
            try
            {
                ApiResult<int> result = new ApiResult<int>();
                var healthcare = healthcareRepo.GetAll().FirstOrDefault(x=>x.HealthcareId==request.HealthcareId);
                healthcare.IsDeleted=true;
                var r = healthcareRepo.Update(healthcare).Result;
                if (r > 0)
                {
                    result.Data = r;
                    result.Code = ApiEnum.OK;
                    result.Msg = "删除保健信息成功";
                }
                else
                {
                    result.Data = r;
                    result.Code = ApiEnum.Fail;
                    result.Msg = "删除保健信息失败";
                }
                return Task.FromResult(result);
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
